package cn.yuanqiao.manage.service.impl;

import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.manage.domain.YqFields;
import cn.yuanqiao.manage.domain.YqdaMdataGroup;
import cn.yuanqiao.manage.domain.YqdaMdataList;
import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.manage.mapper.YqFieldsMapper;
import cn.yuanqiao.manage.mapper.YqdaMdataGroupMapper;
import cn.yuanqiao.manage.mapper.YqdaMdataListMapper;
import cn.yuanqiao.manage.service.IYqdaMdataGroupService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 元数据方案Service业务层处理
 * 
 * @author yuanqiao
 * @date 2023-06-06
 */
@Service
public class YqdaMdataGroupServiceImpl implements IYqdaMdataGroupService
{
    @Autowired
    private YqdaMdataGroupMapper yqdaMdataGroupMapper;
    @Autowired
    private YqFieldsMapper yqFieldsMapper;
    @Autowired
    private YqdaMdataListMapper yqdaMdataListMapper;

    /**
     * 查询元数据方案
     * 
     * @param ID 元数据方案主键
     * @return 元数据方案
     */
    @Override
    public YqdaMdataGroup selectYqdaMdataGroupByID(Long ID)
    {
        return yqdaMdataGroupMapper.selectYqdaMdataGroupByID(ID);
    }

    /**
     * 查询元数据方案列表
     * 
     * @param yqdaMdataGroup 元数据方案
     * @return 元数据方案
     */
    @Override
    public List<YqdaMdataGroup> selectYqdaMdataGroupList(YqdaMdataGroup yqdaMdataGroup)
    {
        return yqdaMdataGroupMapper.selectYqdaMdataGroupList(yqdaMdataGroup);
    }

    /**
     * 新增元数据方案
     * 
     * @param yqdaMdataGroup 元数据方案
     * @return 结果
     */
    @Override
    @Transactional
    public int insertYqdaMdataGroup(YqdaMdataGroup yqdaMdataGroup)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        yqdaMdataGroup.setCreatorId(user.getUserId());
        yqdaMdataGroup.setCreateTime(DateUtils.getNowDate());
        yqdaMdataGroup.setIsUse(0);
        int r= yqdaMdataGroupMapper.insertYqdaMdataGroup(yqdaMdataGroup);
        if(r>0){
            YqFields yqFields = new YqFields();
            List<YqFields> getGroupDataList = new ArrayList<>();
            yqFields.setIsCommon("1");
            //yqfields.setType("0"); //0件 1卷 2电子文件 3项目
            String type = yqdaMdataGroup.getType() == 3 ? ("0,1,2,3"):(yqdaMdataGroup.getType() == 1 ? ("0,1,2"):("0,2"));
            yqFields.setType(type);
            getGroupDataList.addAll(yqFieldsMapper.getDataByIds(yqFields));

            //查询件的公共字段
            /*getGroupDataList.addAll(yqfieldsMapper.getDataByIds(yqfields));
            yqfields.setType("2");
            getGroupDataList.addAll(yqfieldsMapper.getDataByIds(yqfields));
            if(YqdaMdataGroup.getType() == 1 || YqdaMdataGroup.getType() == 3){
                yqfields.setType("1");
                getGroupDataList.addAll(yqfieldsMapper.getDataByIds(yqfields));
            }
            if(YqdaMdataGroup.getType() == 3){
                yqfields.setType("3");
                getGroupDataList.addAll(yqfieldsMapper.getDataByIds(yqfields));
            }*/
            for(YqFields data:getGroupDataList){
                //添加元数据明细
                YqdaMdataList mDataList = new YqdaMdataList();
                mDataList.setFieldCaption(data.getFieldCaption());
                mDataList.setGroupId(yqdaMdataGroup.getId());
                mDataList.setFieldName(data.getFieldName());
                mDataList.setFieldType(Integer.valueOf(data.getFieldType()));
                mDataList.setDataLength(Integer.valueOf(data.getFieldLength()));
                mDataList.setType(Integer.valueOf(data.getType()));
                mDataList.setShowType(Integer.valueOf(data.getShowType()));
                mDataList.setIsSystem(1);

                mDataList.setDefaultValue(data.getDefaultVal());
                mDataList.setIsNoEmpty("true".equals(data.getNullable()) ? 0 : 1);

                //mDataList.setIsInput("true".equals(data.getNullAble()) ? 0 : 1);
                mDataList.setIsInput(0);
                mDataList.setIsUpdate(0);
                mDataList.setIsQuery(0);
                mDataList.setIsArchiveNo(0);
                // mDataList.setIsNoEmpty(0);
                mDataList.setIsHidden(0);
                mDataList.setIsExport(1);
                mDataList.setCheckChongfu(0);
                mDataList.setIsImport(0);
                mDataList.setIsMetadata(0);

                // mDataList.setShowType(4);
                mDataList.setZsfl("1");
                mDataList.setThWidth(StringUtils.isNotEmpty(data.getThWidth()) ? Integer.valueOf(data.getThWidth()):null);
                mDataList.setThAlign(data.getThAlign());
                mDataList.setCreatorId(user.getUserId());
                mDataList.setCreateTime(DateUtils.getNowDate());
                yqdaMdataListMapper.insertYqdaMdataList(mDataList);
            }

        }
        return r;
    }

    /**
     * 修改元数据方案
     * 
     * @param yqdaMdataGroup 元数据方案
     * @return 结果
     */
    @Override
    public int updateYqdaMdataGroup(YqdaMdataGroup yqdaMdataGroup)
    {
        return yqdaMdataGroupMapper.updateYqdaMdataGroup(yqdaMdataGroup);
    }

    /**
     * 批量删除元数据方案
     * 
     * @param IDs 需要删除的元数据方案主键
     * @return 结果
     */
    @Override
    public int deleteYqdaMdataGroupByIDs(Long[] IDs)
    {
        return yqdaMdataGroupMapper.deleteYqdaMdataGroupByIDs(IDs);
    }

    /**
     * 删除元数据方案信息
     * 
     * @param ID 元数据方案主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteYqdaMdataGroupByID(Long ID)
    {
        yqdaMdataListMapper.deleteYqdaMdataListByGroupId(ID);
        //relatedelementMapper.deleteHdListRelatedelementByGroupId(ID);
        //subelementMapper.deleteHdListSubelementByGroupId(ID);
        return yqdaMdataGroupMapper.deleteYqdaMdataGroupByID(ID);
    }

    @Override
    public List<TreeSelect> selectGroupTreeList(YqdaMdataGroup yqdaMdataGroup) {
        List<YqdaMdataGroup> list = selectYqdaMdataGroupList(yqdaMdataGroup);
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("元数据方案");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdaMdataGroup group:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(group.getId());
            chilTree.setLabel(group.getName());
            chilTree.setType(group.getType());
            chilTree.setIsUse(group.getIsUse());
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }

    @Override
    public List<YqdaMdataGroup> copyGroupList(Long groupId, Integer type) {
        return yqdaMdataGroupMapper.copyGroupList(groupId,type);
    }
}
